﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        static Map m;
        static bool fini = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (_Default.m == null)
            {
                _Default.m = new Map();
                m.init();
                System.Diagnostics.Debug.WriteLine("Map - INIT");
            }

            coords.Text = "Position : (" + m.xJoueur + ", " + m.yJoueur + ") | Vaisseau = " + m.vaisseau;
            dessinerMap();
        }

        private void dessinerMap() {
            // Centre = Vaisseau
            int r, c;
            for (r = 1; r <= 5; r++)
            {
                for (c = 1; c <= 5; c++)
                {
                    int x = c - 3;
                    int y = r - 3;

                    Control cont = Table1.FindControl("Cell" + r + "_" + c);
                    if (cont != null)
                    {
                        Case ca = m.getCell(x, y);
                        Image i = new Image();
                        i.ImageUrl = "inconnue.png";

                        if (ca.GetType().Name == typeof(CaseAsteroide).Name)
                            i.ImageUrl = "asteroids.jpg";
                        else if (ca.GetType().Name == typeof(CaseDebris).Name)
                            i.ImageUrl = "debris.jpg";
                        else if (ca.GetType().Name == typeof(CaseFight).Name)
                            i.ImageUrl = "ship_2.jpg";
                        else if (ca.GetType().Name == typeof(CaseVide).Name)
                            i.ImageUrl = "vide.png";

                        if((cont as TableCell).Controls.Count > 0)
                            (cont as TableCell).Controls.RemoveAt(0);

                        (cont as TableCell).Controls.Add(i);
                    }
                }
            }

            (Cell3_3.Controls[0] as Image).ImageUrl = "ship.jpg";

            Case caseActive = m.getCell(0, 0);

            caseShip.Text = "" + caseActive;

            Image im = new Image();
            if (caseShipPan.Controls.Count > 0)
                caseShipPan.Controls.RemoveAt(0);

            if (caseActive.GetType().Name == typeof(CaseAsteroide).Name)
                im.ImageUrl = "asteroids.jpg";
            else if (caseActive.GetType().Name == typeof(CaseDebris).Name)
                im.ImageUrl = "debris.jpg";
            else if (caseActive.GetType().Name == typeof(CaseFight).Name)
                im.ImageUrl = "ship_2.jpg";
            else if (caseActive.GetType().Name == typeof(CaseVide).Name)
                im.ImageUrl = "vide.png";

            caseShipPan.Controls.Add(im);

            if (fini)
            {
                Button1.Enabled = false;
                Button2.Enabled = false;
                Button3.Enabled = false;
                Button4.Enabled = false;
            }
        }

        protected void cmdDeplacer(object sender, CommandEventArgs e)
        {
            Map.DeplacementEnum deplacement = (Map.DeplacementEnum) Enum.Parse(typeof(Map.DeplacementEnum), e.CommandArgument.ToString(), true);
            m.deplacement(deplacement);
            coords.Text = "Position : (" + m.xJoueur + ", " + m.yJoueur + ") | Vaisseau = " + m.vaisseau;

            if (!m.checkWin())
            {
                m.map[m.xJoueur, m.yJoueur] = m.genererCase();
                m.map[m.xJoueur, m.yJoueur].declencherEffet(m);
            }
            else
                fini = true;

            dessinerMap();
        }

        /*
            int cpt=0;
            int fight = 0, vide = 0, debris = 0, asteroide = 0;
             do
             {            
                if (m.map[m.xJoueur, m.yJoueur] is CaseVide) vide++;
                else if (m.map[m.xJoueur, m.yJoueur] is CaseDebris) debris++;
                else if (m.map[m.xJoueur, m.yJoueur] is CaseAsteroide) asteroide++;
                else if (m.map[m.xJoueur, m.yJoueur] is CaseFight) fight++;
                cpt++;
            } while (!m.jouer());


             for (int i = 0; i < 15; i++)
             {
                 for (int j = 0; j < 15; j++)
                 {
                     if (i == m.xJoueur && j == m.yJoueur) System.Diagnostics.Debug.Write("J ");
                     else if (m.map[i, j] is CaseVide) System.Diagnostics.Debug.Write("O ");
                     else if (m.map[i, j] is CaseFinale) System.Diagnostics.Debug.Write("F ");
                     else if (m.map[i, j] is CaseDebris) System.Diagnostics.Debug.Write("D ");
                     else if (m.map[i, j] is CaseAsteroide) System.Diagnostics.Debug.Write("A ");
                     else if (m.map[i, j] is CaseFight) System.Diagnostics.Debug.Write("X ");
                 }
                 System.Diagnostics.Debug.WriteLine("");
             }
             System.Diagnostics.Debug.WriteLine("");
             System.Diagnostics.Debug.WriteLine("");
             System.Diagnostics.Debug.WriteLine("---------------------------------------------------");
             System.Diagnostics.Debug.WriteLine("");
             System.Diagnostics.Debug.WriteLine("");

             System.Diagnostics.Debug.WriteLine("vide = " + vide);
             System.Diagnostics.Debug.WriteLine("asteroide = " + asteroide);
             System.Diagnostics.Debug.WriteLine("debris = " + debris);
             System.Diagnostics.Debug.WriteLine("fight = " + fight);
             System.Diagnostics.Debug.WriteLine("cpt = " + cpt);
             
        }*/
    }
}
